<?php

##################################################
#
# Copyright (c) 2008-2009 ABSoft, Inc.
#
# This file is part of ABSOft Framework (builded on Exponent)
# Owner Info Module
# 
##################################################

class ownermodule {
	function name() { return 'Owner Info Module'; }
	function description() { return 'Hiện ra trang giới thiệu tiểu sử/giới thiệu chủ webiste'; }
	function author() { return 'ABSoft Company'; }
	
	function hasSources() { return true; }
	function hasContent() { return true; }
	function hasViews() { return true; }
	
	function supportsWorkflow() { return false; }
	
	function permissions($internal = '') {
		if ($internal == '') {
			return array(
				'administrate'=>'Administrate',
				'configure'=>'Configure',
			);
		} else {
			return array(
				'administrate'=>'Administrate',
				'configure'=>'Configure',
			);
		}
	}
	
	function show($view,$loc = null, $title = '') {
		global $db;
		if (!defined('SYS_FILES')) include_once(BASE.'subsystems/files.php');
		$config = $db->selectObject('owner_info',"location_data='".serialize($loc)."'");
		if (!$config) {
			$config->id=-1; // no data
		}
		
		$template = new template('ownermodule',$view,$loc);
		
		
		if ($config && $config->file_image_id <> 0) {
			$file = $db->selectObject('file','id='.$config->file_image_id);
			$config->file_image_path = BASE.$file->directory.'/'.$file->filename;
		}
		
		if ($config && $config->file_pdf_id <> 0) {
			$file = $db->selectObject('file','id='.$config->file_pdf_id);
			$config->file_pdf_path = BASE.$file->directory.'/'.$file->filename;
		}
				
		$template->assign('moduletitle',$title);
		$template->assign('config',$config);
		$template->register_permissions(
			array('administrate','user','configure'),
			$loc);
		
		$template->output();
	}
	
	function deleteIn($loc) {
		global $db;
		$config = $db->selectObject('owner_info',"location_data='".serialize($loc)."'");
		if ($config) {
			if ($config->file_image_id != 0) {
				$file = $db->selectObject('file','id='.$config->file_image_id);
				if ($file) {
					file::delete($file);
					rmdir(BASE.$file->directory);
				}
			}
			if ($config->file_pdf_id != 0) {
				$file = $db->selectObject('file','id='.$config->file_pdf_id);
				if ($file) {
					file::delete($file);
					rmdir(BASE.$file->directory);
				}
			}
			$db->delete('owner_info',"location_data='".serialize($loc)."'");
		}
	}
	
	function copyContent($oloc,$nloc) {
		global $db;
	
		$dir = "files/ownermodule/".$nloc->src;
		if (!file_exists(BASE.$dir)) {
			if (!defined("SYS_FILES")) require_once(BASE."subsystems/files.php");
			exponent_files_makeDirectory($dir);
		}
		
		if (!file_exists(BASE.$dir)) {
			return;
		}
		
		$config = $db->selectObject('owner_info',"location_data='".serialize($oloc)."'");
		if ($config) {
			$file1 = $db->selectObject('file','id='.$config->file_image_id);
			$file2 = $db->selectObject('file','id='.$config->file_pdf_id);
			if ($file1 || $file2) {
				copy(BASE.$file1->directory.'/'.$file1->filename,BASE.$dir.'/'.$file1->filename);
				copy(BASE.$file2->directory.'/'.$file2->filename,BASE.$dir.'/'.$file2->filename);
				if (file_exists(BASE.$dir.'/'.$file1->filename)) {
					$file1->directory = $dir;
					unset($file1->id);
					$config->file_image_id = $db->insertObject($file,'file');
				}
				if (file_exists(BASE.$dir.'/'.$file2->filename)) {
					$file2->directory = $dir;
					unset($file->id);
					$config->file_pdf_id = $db->insertObject($file,'file');
				}
				$config->location_data = serialize($nloc);
				$db->insertObject($config,'owner_info');
			}
		}
	}
	
	function spiderContent($item = null) {
		// FIXME:searching in page module.
		return false;
	}
}

?>
